/**   
 * Copyright © 2016  All rights reserved.
 * 
 * @Title: NettyCommandServer.java 
 * @Project: clusterntss
 * @Package: com.jndk.netty.json 
 * @Description: 请用一句话描述
 * @author: yuanzhenghe
 * @date: 2016年12月9日 上午11:24:00 
 * @version: V1.0   
 */
package com.jndk.netty.json;

import java.nio.ByteOrder;
import java.nio.charset.Charset;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.logging.LoggingHandler;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

/**
 * @ClassName: NettyCommandServer
 * @Description: 请用一句话描述
 * @author: yuanzhenghe
 * @date: 2016年12月9日 上午11:24:00
 */
public class NettyCommandServer {

	public static InternalLogger logger = InternalLoggerFactory.getInstance(NettyCommandServer.class);

	public void bind(int port) throws Exception {
		EventLoopGroup bossGroup = new NioEventLoopGroup();
		EventLoopGroup workerGroup = new NioEventLoopGroup();
		try {
			ServerBootstrap serverBootstrap = new ServerBootstrap();
			serverBootstrap.group(bossGroup, workerGroup);
			serverBootstrap.channel(NioServerSocketChannel.class);
			serverBootstrap.option(ChannelOption.SO_BACKLOG, 1024);
			serverBootstrap.handler(new LoggingHandler());
			serverBootstrap.childHandler(new NettyChannelHandler());
			ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
			channelFuture.channel().closeFuture().sync();
		} finally {
			bossGroup.shutdownGracefully();
			workerGroup.shutdownGracefully();
		}
	}

	private class NettyChannelHandler extends ChannelInitializer<SocketChannel> {

		@Override
		protected void initChannel(SocketChannel socketChannel) throws Exception {
			socketChannel.pipeline()
					.addLast(new LengthFieldBasedFrameDecoder(ByteOrder.BIG_ENDIAN, 64 * 1024, 0, 4, 0, 4, true));
			socketChannel.pipeline().addLast(new StringDecoder(Charset.forName("UTF-8")));
			socketChannel.pipeline().addLast(new NettyCommandHandler());
		}
	}
}
